package bar.dark.lee.alarm.main;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import bar.dark.lee.alarm.BDContents;
import bar.dark.lee.alarm.R;
import bar.dark.lee.alarm.SuperActivity;
import bar.dark.lee.alarm.add.AddActivity;
import bar.dark.lee.alarm.common.BDTextView;
import bar.dark.lee.alarm.common.BDUtil;
import bar.dark.lee.alarm.common.SuperInterface;
import bar.dark.lee.alarm.query.AlarmData;
import bar.dark.lee.alarm.query.DBQuery;

public class MainActivity extends SuperActivity
{
	private Button btnAdd;
	private ListView listView;
	
	private ArrayList<AlarmData> list = null;
	
	private View resultView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main_activity);
		
		setTitle(getString(R.string.main_title));
		
		setFindView();
		setClickListener();
	}
	@Override
	protected void onResume()
	{
		super.onResume();
		new ShowDialog().execute(0);
	}

	@Override
	public void setFindView() {
		btnAdd = (Button) findViewById(R.id.main_btnAdd);
		listView = (ListView) findViewById(R.id.main_list);
		
		LayoutInflater li = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		resultView = li.inflate(R.layout.view_no_result, null);
	}

	@Override
	public void setDataSetting() 
	{
		list = query.getAlarmList(superContext);
	}

	@Override
	public void setClickListener() 
	{
		btnAdd.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				Intent intent = new Intent(superContext, AddActivity.class);
				startActivity(intent);
			}
		});
	}
	
	private OnItemClickListener onItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
				long arg3)
		{
			AlarmData data = list.get(pos);
			
			Intent intent = new Intent(superContext, InfoActivity.class);
			intent.putExtra(DBQuery.ALARMDATA, data);
			startActivity(intent);
		}
	};
	
	private class AlarmAdapter extends ArrayAdapter<AlarmData>
	{
		private ArrayList<AlarmData> list;
		
		public AlarmAdapter(Context context, int textViewResourceId,
				ArrayList<AlarmData> list)
		{
			super(context, textViewResourceId, list);
			this.list = list;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View v = convertView;
			
			if (v == null)
			{
				v = getLayoutInflater().inflate(R.layout.list_item_alarm, null);
			}
			
			AlarmData data = list.get(position);
			
			BDTextView icon = (BDTextView) v.findViewById(R.id.listAlarmIcon);
			BDTextView time = (BDTextView) v.findViewById(R.id.listAlarmTime);
			BDTextView weekday = (BDTextView) v.findViewById(R.id.listAlarmWeekDay);
			
			icon.setText(data.getEffectName());
			time.setText(data.getTimeName());
			weekday.setText(data.getDateName());
			
			return v;
		}
	}
	
	/**
	 * 다이얼로그 뛰우기 
	 */
	private class ShowDialog extends AsyncTask<Integer, Integer, Integer>{
		ProgressDialog dialog = null;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			dialog = new ProgressDialog(MainActivity.this);
			dialog.setCancelable(false);
			dialog.setMessage("Loading...");
			dialog.show();
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) {
			if (arg0[0] == 0)
			{
				setDataSetting();
				return 0;
			}

			return -1;
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);

			if (dialog != null) {
				dialog.dismiss();
			}
			
			if (result == 0)
			{
				if (listView.getFooterViewsCount() > 0)
				{
					listView.removeFooterView(resultView);
				}
				
				if (list.size() < 1)
				{
					listView.addFooterView(resultView);
					listView.setFooterDividersEnabled(false);
				}
				else
				{
					listView.setOnItemClickListener(onItemClickListener);
					listView.setDividerHeight(1);
				}
				
				AlarmAdapter adapter = new AlarmAdapter(superContext, R.layout.list_item_alarm, list);
				listView.setAdapter(adapter);
				adapter.notifyDataSetChanged();
			}
		}
	}
	
	@Override
    public void onBackPressed()
    {
    	AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    	dialog.setIcon(android.R.drawable.ic_dialog_alert);
    	dialog.setTitle("어플종료하기");
    	dialog.setMessage("어플을 종료 하시겠습니까?");
    	dialog.setPositiveButton("종료", new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				sql.closeDatabese();
				dialog.dismiss();
				finish();
			}
		});
    	dialog.setNegativeButton("취소", null);
    	
    	dialog.show();
    }
	
	@Override
	protected void tipButton() {
		
	}
}
